package cn.thoughtworks.school.usercenter.entities;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class WrappedUser {

    private Map<String, Object> wrappedUser = new HashMap<>();

    public static List<Map> merge(List<User> users, List<UserDetail> userDetails) {
        List<WrappedUser> wrappedUsers = new ArrayList<>();
        for (User user : users) {
            WrappedUser wrapped = new WrappedUser();
            wrapped.wrap(user);
            for(UserDetail detail: userDetails) {
                if(detail.getUserId() == user.getId()) {
                    wrapped.wrap(detail);
                    break;
                }
            }

            wrappedUsers.add(wrapped);
        }

        return wrappedUsers.stream().map(user -> user.get()).collect(Collectors.toList());
    }

    public WrappedUser wrap(User user) {
        wrappedUser.put("id", user.getId());
        wrappedUser.put("username", user.getUsername());
        wrappedUser.put("email", user.getEmail());
        wrappedUser.put("mobilePhone", user.getMobilePhone());
        wrappedUser.put("createDate", user.getCreateDate());

        return this;
    }

    public WrappedUser wrap(UserDetail userDetail) {
        if (userDetail == null) return this;

        wrappedUser.put("name", userDetail.getName());
        wrappedUser.put("school", userDetail.getSchool());
        wrappedUser.put("major", userDetail.getMajor());

        return this;
    }

    public Map<String, Object> get() {
        return wrappedUser;
    }
}
